<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  <head>
    <title>Variables and Patterns</title>
    <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
    <link rel="stylesheet" type="text/css" href="kawa.css"/>
    <script src="kawa-ebook.js" type="text/javascript"/>
    <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
    <link rel="prev" href="Overall-Index.xhtml" title="Index"/>
    <link rel="next" href="Definitions.xhtml" title="Definitions"/>
  </head>
  <body>
    <header/>
    <section class="sect1" title="Variables and Patterns" epub:type="subchapter" id="Variables-and-Patterns">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both">Variables and Patterns</h2>
          </div>
        </div>
      </div>
      <p>An identifier can name either a type of syntax or a location
where a value can be stored. An identifier that names a
type of syntax is called a <em class="firstterm">syntactic keyword</em>
(informally called a <em class="firstterm">macro</em>), and is said to be
<em class="firstterm">bound</em> to a transformer for that syntax. An identifier that
names a location is called a <em class="firstterm">variable</em> and is said to be <em class="firstterm">bound</em>
to that location. The set of all visible bindings in effect at
some point in a program is known as the <em class="firstterm">environment</em> in
effect at that point. The value stored in the location to
which a variable is bound is called the variable’s value.
By abuse of terminology, the variable is sometimes said
to name the value or to be bound to the value. This is
not quite accurate, but confusion rarely results from this
practice.
</p>
      <p>Certain expression types are used to create new kinds of
syntax and to bind syntactic keywords to those new syntaxes,
while other expression types create new locations
and bind variables to those locations. These expression
types are called <em class="firstterm">binding constructs</em>.
Those that bind syntactic keywords are discussed in <a class="link" href="Macros.xhtml" title="Macros">Macros</a>.
The most fundamental of the variable binding constructs is the
<a class="link" href="Extended-formals.xhtml#meta-lambda-expression"><code class="literal">lambda</code> expression</a>,
because all other variable binding constructs
can be explained in terms of <code class="literal">lambda</code> expressions.
Other binding constructs include the <a class="link" href="Definitions.xhtml" title="Definitions"><code class="literal">define</code> family</a>,
and the <a class="link" href="Local-binding-constructs.xhtml" title="Local binding constructs"><code class="literal">let</code> family</a>.
</p>
      <p>Scheme is a language with block structure. To each place
where an identifier is bound in a program there corresponds
a <em class="firstterm">region</em> of the program text within which the binding is visible.
The region is determined by the particular binding construct that
establishes the binding; if the binding is
established by a <code class="literal">lambda</code> expression, for example, then its
region is the entire <code class="literal">lambda</code> expression. Every mention of
an identifier refers to the binding of the identifier that established
the innermost of the regions containing the use.
</p>
      <p>If there is no binding of the identifier whose region contains the use,
then the use refers to the binding for the
variable in the global environment, if any;
if there is no binding for the identifier, it is said to be <em class="firstterm">unbound</em>.
</p>
      <section class="sect2" title="Patterns" epub:type="division" id="idm139667878322480">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title">Patterns</h3>
            </div>
          </div>
        </div>
        <p>The usual way to bind variables is to match an incoming
value against a <em class="firstterm">pattern</em>.  The pattern contains variables
that are bound to some value derived from the value.
</p>
        <pre class="screen">(! [x::double y::double] (some-expression))
</pre>
        <p>In the above example, the pattern <code class="literal">[x::double y::double]</code>
is matched against the incoming value that results from
evaluating <code class="literal">(some-expression)</code>.
That value is required to be a two-element sequence.
Then the sub-pattern <code class="literal">x::double</code> is matched against
element 0 of the sequence, which means it is coerced to a <code class="literal">double</code>
and then the coerced value is matched against the sub-pattern <code class="literal">x</code>
(which trivially succeeds).  Similarly, <code class="literal">y::double</code> is matched
against element 1.
</p>
        <p>The syntax of patterns is a work-in-progress. (The focus until now
has been in designing and implementing how patterns work in general,
rather than the details of the pattern syntax.)
</p>
        <div class="literallayout">
          <p><a id="idm139667878316272" class="indexterm"/><span id="meta-pattern"/><em class="replaceable"><code>pattern</code></em> <code class="literal">::=</code> <a class="link" href="Lexical-syntax.xhtml#meta-identifier"><em class="replaceable"><code>identifier</code></em></a><br/>
  | <code class="literal"><span class="bold"><strong>_</strong></span></code><br/>
  | <a class="link" href="Variables-and-Patterns.xhtml#meta-pattern-literal"><em class="replaceable"><code>pattern-literal</code></em></a><br/>
  | <code class="literal"><span class="bold"><strong>’</strong></span></code><a class="link" href="Datum-syntax.xhtml#meta-datum"><em class="replaceable"><code>datum</code></em></a><br/>
  | <a class="link" href="Variables-and-Patterns.xhtml#meta-pattern"><em class="replaceable"><code>pattern</code></em></a> <code class="literal"><span class="bold"><strong>::</strong></span></code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a><br/>
  | <code class="literal"><span class="bold"><strong>[</strong></span></code> <a class="link" href="Variables-and-Patterns.xhtml#meta-lpattern"><em class="replaceable"><code>lpattern</code></em></a><sup>*</sup> <code class="literal"><span class="bold"><strong>]</strong></span></code><br/>
<a id="idm139667878305152" class="indexterm"/><span id="meta-lpattern"/><em class="replaceable"><code>lpattern</code></em> <code class="literal">::=</code> <a class="link" href="Variables-and-Patterns.xhtml#meta-pattern"><em class="replaceable"><code>pattern</code></em></a><br/>
  | <code class="literal"><span class="bold"><strong>@</strong></span></code> <a class="link" href="Variables-and-Patterns.xhtml#meta-pattern"><em class="replaceable"><code>pattern</code></em></a><br/>
  | <a class="link" href="Variables-and-Patterns.xhtml#meta-pattern"><em class="replaceable"><code>pattern</code></em></a> <code class="literal"><span class="bold"><strong>...</strong></span></code><br/>
  | <a class="link" href="Variables-and-Patterns.xhtml#meta-guard"><em class="replaceable"><code>guard</code></em></a><br/>
<a id="idm139667878298240" class="indexterm"/><span id="meta-pattern-literal"/><em class="replaceable"><code>pattern-literal</code></em> <code class="literal">::=</code><br/>
    <a class="link" href="Boolean-values.xhtml#meta-boolean"><em class="replaceable"><code>boolean</code></em></a> | number | <a class="link" href="Characters.xhtml#meta-character"><em class="replaceable"><code>character</code></em></a> | <a class="link" href="String-literals.xhtml#meta-string"><em class="replaceable"><code>string</code></em></a><br/>
<a id="idm139667878293776" class="indexterm"/><span id="meta-guard"/><em class="replaceable"><code>guard</code></em> <code class="literal">::=</code> <code class="literal"><span class="bold"><strong>#!if</strong></span></code> <a class="link" href="Primitive-expression-syntax.xhtml#meta-expression"><em class="replaceable"><code>expression</code></em></a><br/>
</p>
        </div>
        <p>This is how the specific patterns work:
</p>
        <div class="variablelist" epub:type="list">
          <dl class="variablelist">
            <dt class="term"><a class="link" href="Lexical-syntax.xhtml#meta-identifier"><em class="replaceable"><code>identifier</code></em></a>
</dt>
            <dd>
              <p>This is the simplest and most common form of pattern.
The <em class="replaceable"><code>identifier</code></em> is bound to a new variable
that is initialized to the incoming value.
</p>
            </dd>
            <dt class="term"><code class="literal"><span class="bold"><strong>_</strong></span></code>
</dt>
            <dd>
              <p>This pattern just discards the incoming value.
It is equivalent to a unique otherwise-unused <em class="replaceable"><code>identifier</code></em>.
</p>
            </dd>
            <dt class="term"><a class="link" href="Variables-and-Patterns.xhtml#meta-pattern-literal"><em class="replaceable"><code>pattern-literal</code></em></a>
</dt>
            <dd>
              <p>Matches if the value is <code class="literal">equal?</code> to the <em class="replaceable"><code>pattern-literal</code></em>.
</p>
            </dd>
            <dt class="term"><code class="literal"><span class="bold"><strong>’</strong></span></code><a class="link" href="Datum-syntax.xhtml#meta-datum"><em class="replaceable"><code>datum</code></em></a>
</dt>
            <dd>
              <p>Matches if the value is <code class="literal">equal?</code> to the quoted <em class="replaceable"><code>datum</code></em>.
</p>
            </dd>
            <dt class="term"><a class="link" href="Variables-and-Patterns.xhtml#meta-pattern"><em class="replaceable"><code>pattern</code></em></a> <code class="literal"><span class="bold"><strong>::</strong></span></code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a>
</dt>
            <dd>
              <p>The incoming value is coerced to a value of the specified <em class="replaceable"><code>type</code></em>,
and then the coerced value is matched against the sub-<em class="replaceable"><code>pattern</code></em>.
Most commonly the sub-<em class="replaceable"><code>pattern</code></em> is a plain <em class="replaceable"><code>identifier</code></em>,
so the latter match is trivial.
</p>
            </dd>
            <dt class="term"><code class="literal"><span class="bold"><strong>[</strong></span></code> <a class="link" href="Variables-and-Patterns.xhtml#meta-lpattern"><em class="replaceable"><code>lpattern</code></em></a><sup>*</sup> <code class="literal"><span class="bold"><strong>]</strong></span></code>
</dt>
            <dd>
              <p>The incoming value must be a sequence (a list, vector or similar).
In the case where each sub-pattern is a plain <em class="replaceable"><code>pattern</code></em>,
then the number of sub-patterns must match the size of the sequence, and
each sub-pattern is matched against the corresponding element of the sequence.
More generally, each sub-pattern may match zero or more consequtive
elements of the incoming sequence.
</p>
            </dd>
            <dt class="term"><code class="literal"><span class="bold"><strong>#!if</strong></span></code> <a class="link" href="Primitive-expression-syntax.xhtml#meta-expression"><em class="replaceable"><code>expression</code></em></a>
</dt>
            <dd>
              <p>No incoming value is used.  Instead the <em class="replaceable"><code>expression</code></em> is evaluated.
If the result is true, matching succeeds (so far);
otherwise the match fails.
This form is called a <a class="ulink" href="https://en.wikipedia.org/wiki/Guard_(computer_science)" target="_top"><em class="firstterm">guard</em></a>.
</p>
            </dd>
          </dl>
        </div>
      </section>
    </section>
    <footer>
      <div class="navfooter">
        <ul>
          <li>
            <b class="toc">
              <a href="Variables-and-Patterns.xhtml#idm139667878322480">Patterns</a>
            </b>
          </li>
        </ul>
        <p>
          Up: <a accesskey="u" href="Program-structure.xhtml">Program structure</a></p>
        <p>
        Previous: <a accesskey="p" href="Conditionals.xhtml">Conditionals</a></p>
        <p>
        Next: <a accesskey="n" href="Definitions.xhtml">Definitions</a></p>
      </div>
    </footer>
  </body>
</html>
